package com.bdqn.utli;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 忍
 * @title: MyBatidUtil
 * @projectName Teaching
 * @description: TODO
 * @date 2019/5/1011:30
 */
public class MyBatisUtil {
    private static volatile SqlSessionFactory sqlSessionFactory;

    public static SqlSession getSqlSession(){
        try {
            if (sqlSessionFactory == null){
                //读取配置文件
                InputStream input = Resources.getResourceAsStream("mybaits.xml");
                synchronized(MyBatisUtil.class){
                    if (sqlSessionFactory == null){
                        sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSessionFactory.openSession();
    }
}
